/**
 * Request validation of given fields
 * @param {Validator} v validator object
 * @param {type} fields
 * @param {type} step_num
 * @param {type} leaveStepCallback callback function called after step is leaved
 * @param {type} params validation parameter field hashmap
 * @returns {boolean} true if valid
 */
function requestValidation(v, fields, step_num, leaveStepCallback, params) {    
  var valid = true;
  v.validateAll(fields, function(result) {
    if (result) {
      $('#wizard').smartWizard('setError',{stepnum:step_num,iserror:false});
      leaveStepCallback(step_num);
    } else {
      $('#wizard').smartWizard('setError',{stepnum:step_num,iserror:true});  
    }
    valid = result;
  }, params);
  return valid;
}

/**
 * Request support data for fields (via Ajax)
 * @param {string} url
 * @param {type} type
 * @param {type} fields
 * @param {type} objects
 * @param {string} noSelText is displayed in select if no value is selected
 * @returns {unresolved}
 */
function requestSupportData(url, type, fields, objects, noSelText) {  
    
  // If no action type specified or if there are no fields to prepare, exit
  if (type === '' || fields.length === 0) {
    return;
  }  
  
  // Update text for empty select value
  if (noSelText === undefined) {
    noSelText = '';
  }
    
  // Ajax
  $.ajax({
    async: false,
    url: url,
    data: createSupportLink(type, fields, objects),
    dataType: 'json',
    type: 'post',
    success: function(j) {
      if (type === 'select') {
        for (var i = 0; i < fields.length; i++) {      
          var e = document.getElementById(fields[i] + "_select");
          e.innerHTML = newSelect(fields[i], j.records[i], j.ids[i], 
                                  j.names[i], noSelText);
        }
      } else if (type === 'hidden') {
        for (var i = 0; i < fields.length; i++) { 
          var e = document.getElementById(fields[i]);
          e.outerHTML = newHidden(fields[i], j.ids[i]);
        }
      } else if (type === 'disable') {
        for (var i = 0; i < fields.length; i++) { 
          var e = document.getElementById('prop_' + fields[i]);
          e.style.display = Number(j.disable[i]) === 1 ? 'none' : 'inline';
        }
      }
    }
  }); 
}

/**
 * Creates a link for support data request
 * @param {type} type
 * @param {type} fields
 * @param {type} objects
 * @returns {String}
 */
function createSupportLink(type, fields, objects) {    
  var link = "action=" + type + "&fields=" + fields.join("_and_");
  for (var i = 0; i < objects.length; i++) {
    var obj = document.getElementById(objects[i]);
    link = link + "&" + objects[i] + "=" + obj.value;
  }
  return link;
}

/**
 * Creates new select html item
 * @param {type} property
 * @param {type} length
 * @param {type} ids
 * @param {type} names
 * @param {type} emptyValText text that is displayed if empty value is selected
 * @returns {String}
 */
function newSelect(property, length, ids, names, emptyValText) {
    
  // Temporary variables
  var id = property + '_select';
  var updater = 'javascript:' + property + '_updateHidden()';
  var propobj = document.getElementById(property);
  var selval = Number(propobj.value);
  var valfound = false;
    
  // Create HTML select
  var r = '<select id="'+id+'" name="'+id+'" onChange="'+updater+'" >';
  r += '<option value="">'+emptyValText+'</option>';
  for (var i = 0; i < length; i++) {
    var tmp = '';
    if (ids[i] === selval) {
      tmp = 'selected="selected"';
      valfound = true;
    }
    r += '<option value="'+ids[i]+'" '+tmp+'>'+names[i]+'</option>';      
  }
  r += '</select>';
    
  // If value was not found among the select options, empty hidden
  if (valfound === false) {
    propobj.value = '';
    $('#validate_' + property).html("");
  }
  return r;
}

/**
 * Creates new html hidden field
 * @param {type} prop
 * @param {type} val
 * @returns {String}
 */
function newHidden(prop, val) {
  return '<input type="hidden" id="'+prop+'" name="'+prop+'" value="'+val+'" />';
}
